package com.boatAnchorAssistant.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.AlarmCause;
import com.boatAnchorAssistant.pojo.entity.AlarmType;
import com.boatAnchorAssistant.pojo.entity.EventGroup;
import com.boatAnchorAssistant.pojo.param.alarmType.*;
import com.boatAnchorAssistant.pojo.vo.alarmType.*;
import com.boatAnchorAssistant.pojo.dto.alarmType.*;
import com.boatAnchorAssistant.service.check.ClassUtil;
import com.boatAnchorAssistant.service.repository.in.AlarmTypeRepository;
import com.boatAnchorAssistant.service.repository.in.EventGroupRepository;
import com.boatAnchorAssistant.service.service.in.AlarmTypeService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：报警类型服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class AlarmTypeServiceImpl implements AlarmTypeService {

    private final AlarmTypeRepository alarmTypeRepository;
    private final EventGroupRepository eventGroupRepository;
    private final ClassUtil<AlarmType> classUtil;
    public AlarmTypeServiceImpl(AlarmTypeRepository alarmTypeRepository, EventGroupRepository eventGroupRepository, ClassUtil<AlarmType> classUtil) {
        this.alarmTypeRepository = alarmTypeRepository;
        this.eventGroupRepository = eventGroupRepository;
        this.classUtil = classUtil;
    }

    void check(Boolean isSave,AlarmType entity){
        try {
            if (isSave) {
                classUtil.addIsHave(entity, alarmTypeRepository);
            } else {
                classUtil.updateIsHave(entity, alarmTypeRepository,entity.getId());
            }
        }
        catch (Exception e){
            e.printStackTrace();
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 创建报警类型
     *
     * @param param
     */
    @Override
    public void createAlarmType(CreateAlarmTypeParam param) {
        AlarmType alarmType = new AlarmType();
        BeanUtils.copyProperties(param, alarmType);
        check(true,alarmType);
        Long id = IdGenerator.SNOW.generate();
        alarmType.setId(id);
        alarmType.setIsDisable(!param.getEnable());
        alarmTypeRepository.create(alarmType);
    }

    /**
     * 修改报警类型
     *
     * @param param
     */
    @Override
    public void editAlarmType(EditAlarmTypeParam param) {
        Long id = param.getId();
        AlarmType alarmType = alarmTypeRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, alarmType);
        alarmType.setIsDisable(!param.getEnable());
        check(false,alarmType);
        alarmTypeRepository.edit(alarmType);
    }

    /**
     * 移除报警类型
     *
     * @param id
     */
    @Override
    public void removeAlarmType(Long id) {
        alarmTypeRepository.removeById(id);
    }

    /**
     * 启动/禁用报警类型
     *
     * @param id
     */
    @Override
    public void enabledAlarmType(Long id) {
        AlarmType alarmType = alarmTypeRepository.getByIdCheck(id);
        alarmType.setIsDisable(!alarmType.getIsDisable());
        alarmTypeRepository.edit(alarmType);
    }

    /**
     * 获取报警类型详情
     *
     * @param id
     * @return
     */
    @Override
    public GetAlarmTypeVo getAlarmType(Long id) {
        AlarmType alarmType = alarmTypeRepository.getByIdCheck(id);
        GetAlarmTypeVo vo = new GetAlarmTypeVo();
        BeanUtils.copyProperties(alarmType, vo);
        vo.setEnable(!alarmType.getIsDisable());
        return vo;
    }

    /**
     * 获取报警类型 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetAlarmTypeListVo> getAlarmTypeList(GetAlarmTypeListParam param) {

        SelectAlarmTypeDto dto = new SelectAlarmTypeDto();
        BeanUtils.copyProperties(param, dto);
        Page<AlarmType> page = alarmTypeRepository.selectAlarmType(dto);
        List<AlarmType> alarmTypeList = page.getRecords();
        List<GetAlarmTypeListVo> list = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(alarmTypeList)) {
            List<Long> ids = alarmTypeList.stream().map(AlarmType::getEventId).collect(Collectors.toList());
            Map<Long, EventGroup> map = eventGroupRepository.getMap(ids);
            for (AlarmType alarmType : alarmTypeList) {
                GetAlarmTypeListVo vo = new GetAlarmTypeListVo();
                BeanUtils.copyProperties(alarmType, vo);
                if (map != null) {
                    EventGroup eventGroup = map.get(alarmType.getEventId());
                    if (eventGroup != null) {
                        vo.setEventName(eventGroup.getName());
                    }
                }
                vo.setEnable(!alarmType.getIsDisable());
                list.add(vo);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取报警类型下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetAlarmTypeDownListVo> getAlarmTypeDownList(GetAlarmTypeDownListParam param) {
        AlarmType po = new AlarmType();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<AlarmType> alarmTypeList = alarmTypeRepository.getListByEntity(po);

        List<GetAlarmTypeDownListVo> list = new ArrayList<>();
        for (AlarmType alarmType : alarmTypeList) {
            GetAlarmTypeDownListVo vo = new GetAlarmTypeDownListVo();
            BeanUtils.copyProperties(alarmType, vo);
            list.add(vo);
        }
        return list;
    }


}



